package com.jnxg.smartcommunity.utils;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jnxg.smartcommunity.R;
import com.jnxg.smartcommunity.listener.NavListener;


public class NavUtils {

	public static void setTitle(String text, int color, Activity activity) {
		TextView title = (TextView) activity.findViewById(R.id.nav_title);
		View top = (RelativeLayout) activity.findViewById(R.id.top);
		setTitle(top, title, text, color);
	}

	public static void setTitle(String text, int color,
			View view) {
		TextView title = (TextView) view.findViewById(R.id.nav_title);
		RelativeLayout top = (RelativeLayout) view.findViewById(R.id.top);
		setTitle(top, title, text, color);
	}
	
	public static void setTitle(View top, TextView title,
			String text, int color) {
		title.setTextColor(color);
		title.setText(text);
	}

	public static void setTitle(TextView title,
			String text, int color) {
		title.setTextColor(color);
		title.setText(text);
	}
	
	
	public static void setLeftBtn(String text, int color,
			final Activity activity, final NavListener listener) {
		final Button leftBtn = (Button) activity.findViewById(R.id.btn_left);
		final LinearLayout leftBtnLayout = (LinearLayout) activity.findViewById(R.id.btn_left_layout);
		setLeftBtn(leftBtn, leftBtnLayout, text, color, listener);
	}
	
	public static void setLeftBtn(final String text, int color,
			final Activity activity, final NavListener listener, View pView) {
		final Button leftBtn = (Button) pView.findViewById(R.id.btn_left);
		final LinearLayout leftBtnLayout = (LinearLayout) pView.findViewById(R.id.btn_left_layout);
		setLeftBtn(leftBtn, leftBtnLayout, text, color, listener);
	}

	public static void setLeftBtn(final int resid, final Activity activity,
			final NavListener listener) {
		final Button leftBtn = (Button) activity.findViewById(R.id.btn_left);
		final LinearLayout leftBtnLayout = (LinearLayout) activity.findViewById(R.id.btn_left_layout);
		setLeftBtn(leftBtnLayout, leftBtn, resid, activity, listener);
	}

	public static void setLeftBtn(final int resid, final Activity activity,
			final NavListener listener, View view) {
		final Button leftBtn = (Button) view.findViewById(R.id.btn_left);
		final LinearLayout leftBtnLayout = (LinearLayout) view.findViewById(R.id.btn_left_layout);
		setLeftBtn(leftBtnLayout, leftBtn, resid, activity, listener);
	}
	
	private static void setLeftBtn(Button leftBtn, LinearLayout leftBtnLayout, String text, int color,final NavListener listener){
		leftBtn.setText(text);
		leftBtn.setTextColor(color);
		leftBtnLayout.setVisibility(View.VISIBLE);
		leftBtnLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.onClickLeft();
			}
		});
	}

	private static void setLeftBtn(final LinearLayout leftBtnLayout, final Button leftBtn, final int resid,
			final Activity activity, final NavListener listener) {
		leftBtn.setBackgroundResource(resid);
		leftBtnLayout.setVisibility(View.VISIBLE);
		UIUtils.addOnGlobalLayoutListener(leftBtn,
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// TODO Auto-generated method stub
						LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) leftBtn
								.getLayoutParams();
						Drawable drawable = activity.getResources()
								.getDrawable(resid);
						lp.height = leftBtn.getWidth()
								* drawable.getIntrinsicHeight()
								/ drawable.getIntrinsicWidth();
						leftBtn.setLayoutParams(lp);
						UIUtils.removeOnGlobalLayoutListener(leftBtn, this);
					}
				});
		leftBtnLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.onClickLeft();
			}
		});
	}


	public static void setRightBtn(final String text, int color,
			final Activity activity, final NavListener listener) {
		final Button rightBtn = (Button) activity.findViewById(R.id.btn_right);
		final LinearLayout rightBtnLayout = (LinearLayout) activity.findViewById(R.id.btn_right_layout);
		setRightBtn(rightBtn, rightBtnLayout, text, color, listener);
	}
	
	public static void setRightBtn(final String text, int color,
			final Activity activity, final NavListener listener, View pView) {
		final Button rightBtn = (Button) pView.findViewById(R.id.btn_right);
		final LinearLayout rightBtnLayout = (LinearLayout) pView.findViewById(R.id.btn_right_layout);
		rightBtn.setText(text);
		rightBtn.setTextColor(color);
		rightBtnLayout.setVisibility(View.VISIBLE);
		rightBtnLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.onClickRight();
			}
		});
		setRightBtn(rightBtn, rightBtnLayout, text, color, listener);
	}
	
	private static void setRightBtn(Button rightBtn, LinearLayout rightBtnLayout, String text, int color,final NavListener listener){
		rightBtn.setText(text);
		rightBtn.setTextColor(color);
		rightBtnLayout.setVisibility(View.VISIBLE);
		rightBtnLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.onClickRight();
			}
		});
	}


	public static void setRightBtn(final int resid, final Activity activity,
			final NavListener listener) {
		final Button rightBtn = (Button) activity.findViewById(R.id.btn_right);
		final LinearLayout rightBtnLayout = (LinearLayout) activity.findViewById(R.id.btn_right_layout);
		setRightBtn(rightBtnLayout, rightBtn, resid, activity, listener);
	}

	public static void setRightBtn(final int resid, final int color, final Activity activity,
			final NavListener listener, final View view) {
		final Button rightBtn = (Button) view.findViewById(R.id.btn_right);
		final LinearLayout rightBtnLayout = (LinearLayout) view.findViewById(R.id.btn_right_layout);
		rightBtn.setTextColor(color);
		setRightBtn(rightBtnLayout, rightBtn, resid, activity, listener);
	}
	
	public static void setRightBtnBackground(final int resid, final Activity activity, final View view) {
		final Button rightBtn = (Button) view.findViewById(R.id.btn_right);
		rightBtn.setBackgroundResource(resid);
	}
	
	public static void setRightBtnBackground(final int resid, final Activity activity) {
		final Button rightBtn = (Button) activity.findViewById(R.id.btn_right);
		rightBtn.setBackgroundResource(resid);
	}

	public static void setRightBtn(final LinearLayout rightBtnLayout, final Button rightBtn, final int resid,
			final Activity activity, final NavListener listener) {
		rightBtn.setBackgroundResource(resid);
		rightBtnLayout.setVisibility(View.VISIBLE);
		UIUtils.addOnGlobalLayoutListener(rightBtn,
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// TODO Auto-generated method stub
						LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rightBtn
								.getLayoutParams();
						Drawable drawable = activity.getResources()
								.getDrawable(resid);
						lp.height = rightBtn.getWidth()
								* drawable.getIntrinsicHeight()
								/ drawable.getIntrinsicWidth();
						rightBtn.setLayoutParams(lp);
						UIUtils.removeOnGlobalLayoutListener(rightBtn, this);
					}
				});
		rightBtnLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.onClickRight();
			}
		});
	}

}
